<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <!-- 省份 -->
      <el-form-item>
        <el-select
          v-model="searchObj.provinceCode"
          placeholder="请选择省份"
          @change="provinceChanged"
          clearable
        >
          <el-option
            v-for="item in provinceList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- 选择市区选项 -->
      <el-form-item>
        <el-select
          v-model="searchObj.cityCode"
          placeholder="请选择市"
          clearable
        >
          <el-option
            v-for="item in cityList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- 医院名称 -->
      <el-form-item>
        <el-input v-model="searchObj.hosname" placeholder="医院名称"></el-input>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="fetchData()"
        >查询</el-button
      >
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <!-- banner列表 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="序号" width="60px" align="center">
                <template slot-scope="scope">
                          
          <p>{{ page * limit + scope.$index + 1 }}</p>
                  </template
        >
      </el-table-column>
      <el-table-column label="医院logo">
                <template slot-scope="scope">
                  <img
            :src="'data:image/jpeg;base64,' + scope.row.logoData"
            width="80"
          />
                  </template
        >
            </el-table-column
      >

          <el-table-column prop="hosname" label="医院名称" />
          <el-table-column prop="param.hostype" label="等级" width="90" />
          <el-table-column
        prop="param.fullAddress"
        label="详情地址"
        width="230px"
      />
          <el-table-column label="状态" width="80px" align="center">
                <template slot-scope="scope">
                          
          <p>{{ scope.row.status === 0 ? "未上线" : "已上线" }}</p>
                  </template
        >
            </el-table-column
      >
          <el-table-column prop="createTime" label="创建时间" />

          <el-table-column label="操作" width="230px" align="center">
            <template slot-scope="scope">
                  <el-button
            v-if="scope.row.status == 1"
            type="primary"
            size="mini"
            @click="updateStatus(scope.row.id, 0)"
            >下线</el-button
          >
                  <el-button
            v-if="scope.row.status == 0"
            type="danger"
            size="mini"
            @click="updateStatus(scope.row.id, 1)"
            >上线</el-button
          >
          <router-link :to="'/hospSet/hospital/detail/' + scope.row.id">
                <el-button type="primary" size="mini">查看</el-button>
          </router-link>
          <router-link :to="'/hospSet/hospital/schedule/' + scope.row.hoscode">
                <el-button type="primary" size="mini">排班</el-button>
          </router-link>
              
        </template>
            </el-table-column
      >
    </el-table>
    <!-- 分页组件 -->
        <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0;  text-align: center"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="fetchData"
      @size-change="changeSize"
    />
  </div>
</template>

<script>
import hospitalApi from "@/api/hosp";
export default {
  data() {
    return {
      listLoading: true, // 数据是否正在加载
      list: null, // banner列表
      total: 0, // 数据库中的总记录数
      page: 0, // 默认页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询表单对象
      provinceList: [], // 所有省的集合
      cityList: [], // 所以市的集合
      districtList: [],
    };
  },
  /**选择前 */
  created() {
    console.log("list changed");
    this.fetchData();
    hospitalApi.findByDictCode("Province").then((response) => {
      this.provinceList = response.data;
    });
  },
  // 声明的方法
  methods: {
    updateStatus(id, status) {
      hospitalApi.updateStatus(id, status).then((response) => {
        this.fetchData(this.page);
      });
    },
    //加载banner列表数据
    fetchData(page = 0) {
      console.log("翻页..." + page);
      // 异步获取远程数据（ajax）
      this.page = page;
      hospitalApi
        .getPageList(this.page, this.limit, this.searchObj)
        .then((response) => {
          this.list = response.data.content;
          this.total = response.data.totalElements;
          //数据加载并绑定成功
          this.listLoading = false;
        });
    },
    // 当页码发生改变的时候
    changeSize(size) {
      console.log(size);
      this.limit = size;
      this.fetchData(0);
    }, // 重置查询表单
    resetData() {
      console.log("重置查询表单");
      this.searchObj = {};
      this.fetchData();
    },
    provinceChanged() {
      // 先进行清洗cityList的数据
      this.cityList = [];
      this.searchObj.cityCode = null;
      this.districtList = [];
      this.searchObj.districtCode = null;
      hospitalApi
        .findByParentId(this.searchObj.provinceCode)
        .then((response) => {
          console.log(response.data);
          this.cityList = response.data;
        });
    },
  },
};
</script>

<style>
</style>